package com.yiboshi.rehrs.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.yiboshi.rehrs.domain.entity.IdFunction;
import com.yiboshi.rehrs.domain.service.AperBaseService;

import java.io.Serializable;
import java.util.Collection;

/**
 *
 */
public class AperBaseServiceImpl<M extends BaseMapper<T>, T extends IdFunction> extends ServiceImpl<M, T> implements IService<T>, AperBaseService<T> {


    protected Page<T> startPage(int pageNo, int pageSize, boolean isCount) {
        return PageMethod.startPage(pageNo, pageSize, isCount);
    }

    protected void clearPage() {
        PageMethod.clearPage();
    }


    @Override
    public LambdaUpdateWrapper<T> getUpdateWrapper() {
        LambdaUpdateWrapper<T> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.setEntityClass(this.getEntityClass());
        return lambdaUpdateWrapper;
    }

    @Override
    public LambdaQueryWrapper<T> getQueryWrapper() {
        LambdaQueryWrapper<T> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.setEntityClass(this.getEntityClass());
        return lambdaQueryWrapper;
    }


    public boolean deleteLogic(Serializable... ids) {
        UpdateWrapper<T> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.in("id", ids).set("deleted", System.currentTimeMillis());
        return update(objectUpdateWrapper);
    }

    public boolean deleteLogic(Collection<Serializable> ids) {
        UpdateWrapper<T> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.in("id", ids).set("deleted", System.currentTimeMillis());
        return update(objectUpdateWrapper);
    }


    @Override
    public boolean saveOrUpdate(T entity) {
        if (null == entity) {
            return false;
        } else {
            TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
            Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
            Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
            return !StringUtils.checkValNull(idVal) ? this.updateById(entity) : this.save(entity);
        }
    }

}



